/*
 * Copyright (C) 2013 Daniel Treffenstädt <daniel@treffenstaedt.de>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "OPolar3D.h"
#include "OMethods.h"
#include "OVector3D.h"

O_START_NAMESPACE

Polar3D::Polar3D()
{
	m_v		= 0.0f;
	m_h		= 0.0f;
	m_m		= 0.0f;
}

Polar3D::Polar3D(Degree v, Degree h, double m)
{
	m_v		= v;
	m_h		= h;
	m_m		= m;
}

Degree Polar3D::v()
{
	return m_v;
}

Degree Polar3D::h()
{
	return m_h;
}

double Polar3D::magnitude()
{
	return m_m;
}

Polar3D* Polar3D::rotateH(Degree d)
{
	m_h		+= d;
	return this;
}

Polar3D* Polar3D::rotateV(Degree d)
{
	m_v		+= d;
	return this;
}


Vector3D Polar3D::toVector()
{
	double x	= 0;
	double y	= 0;
	double z	= 0;

	double g	= magnitude();

	if (m_v != 90)
	{
		y	= m_v.cos() * magnitude();
		g	= Methods::root(Methods::pow(magnitude()) - Methods::pow(y));
		if (m_v > 90)
		{
			y	*= -1;
		}
	}

	if (m_h < 90 || m_h > 270)
	{
		Degree h	= (m_h > 270)?(m_h-270):m_h;
		z	= h.cos() * g;
	}
	else if (m_h > 90 && m_h < 270)
	{
		Degree h	= m_h > 180?m_h-180:m_h-90;
		z	= (h.cos() * g) * -1;
	}

	if (m_h > 0 && m_h < 180)
	{
		Degree h	= m_h > 90?m_h-90:m_h;
		x	= h.sin() * g;
	}
	else if (m_h > 180 && m_h < 360)
	{
		Degree h	= m_h > 270?m_h-270:m_h-180;
		x	= (h.sin() * g) * -1;
	}
	else if (m_h == 0 || m_h == 360)
	{
		z	= g;
		x	= 0;
	}
	else if (m_h == 90)
	{
		z	= 0;
		x	= g;
	}
	else if (m_h == 180)
	{
		z	= g * -1;
		x	= 0;
	}
	else if (m_h == 270)
	{
		z	= 0;
		x	= g * -1;
	}

	return Vector3D(x, y, z);
}


void Polar3D::operator +=(Polar3D p)
{
	Vector3D v	= toVector() + p.toVector();

	m_v			= v.toPolar().v();
	m_h			= v.toPolar().h();
	m_m			= v.toPolar().magnitude();
}

Polar3D Polar3D::operator +(Polar3D p)
{
	Vector3D v	= toVector() + p.toVector();

	return v.toPolar();
}

void Polar3D::operator -=(Polar3D p)
{
	Vector3D v	= toVector() - p.toVector();

	m_v			= v.toPolar().v();
	m_h			= v.toPolar().h();
	m_m			= v.toPolar().magnitude();
}

Polar3D Polar3D::operator -(Polar3D p)
{
	Vector3D v	= toVector() - p.toVector();

	return v.toPolar();
}

void Polar3D::operator +=(Vector3D v)
{
	(*this)		+= v.toPolar();
}

Polar3D Polar3D::operator +(Vector3D v)
{
	return (*this) + v.toPolar();
}

void Polar3D::operator -=(Vector3D v)
{
	(*this)		-= v.toPolar();
}

Polar3D Polar3D::operator -(Vector3D v)
{
	return (*this) - v.toPolar();
}


O_END_NAMESPACE
